package com.example.service.impl;

import com.example.entity.CurrentUser;
import com.example.entity.Result;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.page.Page;
import com.example.service.UserService;
import com.example.utils.DigestUtil;
import com.example.utils.TokenUtils;
import com.example.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TokenUtils tokenUtils;
    @Override
    public User findUserByCode(String code) {
        return userMapper.findUserByCode(code);
    }

    /**
     * 分页查询用户的业务方法
     * @param page
     * @param user
     */
    @Override
    public void queryUserForPagination(Page page, User user) {
        // 查询符合条件的用户总行数
        int userCount = userMapper.selectCountUserByCondition(user);

        // 查询符合条件的所有用户
        List<User> userList = userMapper.selectUserForPagination(page, user);

        // 封装参数到Page对象
        page.setTotalNum(userCount);
        page.setResultList(userList);
    }

    /**
     * 新增用户的业务方法
     * @param user
     * @return
     */
    @Override
    public Result saveUser(String token, User user) {
        // 查询参数user的UserCode是否已经存在
        if (userMapper.findUserByCode(user.getUserCode()) != null) { // 已经存在
            return Result.err(Result.CODE_ERR_BUSINESS, "用户名已存在！");
        }

        // 获取当前用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 设置用户密码为加密后的密码
        user.setUserPwd(DigestUtil.hmacSign(user.getUserPwd()));
        // 设置当前新增用户的创建人为当前用户
        user.setCreateBy(currentUser.getUserId());
        if (userMapper.insertUser(user) <= 0) {
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("新增用户成功！");
    }

    /**
     * 修改用户状态的业务方法
     * @param user
     * @param token
     * @return
     */
    @Override
    public Result modifyUserStateByUserId(User user, String token) {
        // 获取当前用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 设置被修改用户的修改者
        user.setUpdateBy(currentUser.getUserId());

        // 更新用户的状态
        if (userMapper.updateUserStateByUserId(user) <= 0) {
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("修改成功！");
    }

    /**
     * 根据用户id删除用户的业务方法
     * @param userId
     * @return
     */
    @Override
    public Result deleteUserById(int userId) {
        if (userMapper.deleteUserById(userId) <= 0) {
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("删除用户成功！");
    }

    /**
     * 批量删除用户的业务方法
     * @param userIdList
     * @return
     */
    @Override
    public Result deleteUserByList(List<Integer> userIdList) {
        if (userMapper.deleteUserByList(userIdList) <= 0){
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("删除用户成功！");
    }

    /**
     * 根据userId修改userName的业务方法
     * @param user
     * @return
     */
    @Override
    public Result modifyUserNameByUserId(User user) {
        if (userMapper.updateUserNameByUserId(user) <= 0) {
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("修改用户信息成功！");
    }

    /**
     * 根据userId重置密码的业务方法
     * @param user
     * @return
     */
    @Override
    public Result resetPasswdByUserId(User user) {
        // 按默认的密码进行加密
        user.setUserPwd(DigestUtil.hmacSign(WarehouseConstants.USER_DEFAULTPASSWORD));

        if (userMapper.updatePasswdByUserId(user) <= 0){
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("密码重置成功！");
    }
}
